package com.tuniu.agents.tools.widgets.tool;

import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.tuniu.agents.tools.widgets.WidgetsProperties;
import com.tuniu.agents.util.HttpManager;
import com.tuniu.agents.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;

public class WeatherListTool implements BiFunction<WeatherListTool.Request, ToolContext, WeatherListTool.ReqRespWrap> {

    private static final Logger log = LoggerFactory.getLogger(WeatherListTool.class);


    public static final String TOOLNAME = "weather_list";

    @Autowired
    private HttpManager httpManager;

    private final String url;

    public WeatherListTool(WidgetsProperties properties) {
        this.url = properties.getQueryWeatherInfoUrl();
    }

    @Override
    public ReqRespWrap apply(Request request, ToolContext toolContext) {
        log.info("WeatherListTool#apply param:{}", JsonUtil.toJson(request));
        try {
            Map<String,Object> params = new HashMap<>();
            params.put("cityName", request.cityName);
            params.put("platCode", request.platCode);
            Response response = httpManager.get(url, params, Response.class);
            return new ReqRespWrap(request, response);

        } catch (Exception e) {
            log.error("WeatherListTool#apply error request:{}", JsonUtil.toJson(request), e);
        }
        return null;
    }
    @JsonClassDescription("weather request response wrap")
    public record ReqRespWrap(
            @JsonProperty(required = true, value = "request") @JsonPropertyDescription("weather request") Request request,
            @JsonProperty(value = "response") @JsonPropertyDescription("weather response") Response response
    ){}
    @JsonClassDescription("weather query parameters")
    public record Request(
        @JsonProperty(required = true,value = "cityName") @JsonPropertyDescription("city name") String cityName,
        @JsonProperty(value = "countyName") @JsonPropertyDescription("county name") String countyName,
        @JsonProperty(value = "platCode") @JsonPropertyDescription("platform code 1 Gaode 2 Baidu default Gaode") Integer platCode
    ){}


    @JsonClassDescription("weather response")
    public record Response(
            @JsonProperty(value = "success") @JsonPropertyDescription("is it successful") boolean  success,
            @JsonProperty(value = "errorCode") @JsonPropertyDescription("error code") int errorCode,
            @JsonProperty(value = "msg") @JsonPropertyDescription("error message") String msg,
            @JsonProperty(value = "data") @JsonPropertyDescription("weather information list") List<Weather> data
    ){}

    @JsonClassDescription("weather information")
    public record Weather(
            @JsonProperty(value = "date") @JsonPropertyDescription("date") String date,
            @JsonProperty(value = "week") @JsonPropertyDescription("week") String week,
            @JsonProperty(value = "dayweather") @JsonPropertyDescription("day weather") String dayweather,
            @JsonProperty(value = "nightweather") @JsonPropertyDescription("night weather") String nightweather,
            @JsonProperty(value = "daytemp") @JsonPropertyDescription("day temperature") String daytemp,
            @JsonProperty(value = "nighttemp") @JsonPropertyDescription("night temperature") String nighttemp,
            @JsonProperty(value = "daywind") @JsonPropertyDescription("day wind") String daywind,
            @JsonProperty(value = "nightwind") @JsonPropertyDescription("night wind") String nightwind,
            @JsonProperty(value = "daypower") @JsonPropertyDescription("day power") String daypower,
            @JsonProperty(value = "nightpower") @JsonPropertyDescription("night power") String nightpower
    ){}

}
